/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package YottaTron.Behaviors;

import YottaTron.Actions.*;

import YottaTron.Runner;
import YottaTron.Sprites.BotSprite;

/**
 *
 * @author Cenda
 */
public class GoToBehavior extends Behavior{
    BotSprite sprite;
    public GoToBehavior(BotSprite s, int xDest, int yDest, Runner runner){
    this.sprite=s;
    this.runner = runner;

    //uhel prepocteny pro potreby atan2
    double currentAngle = sprite.getDirection();
    //spocitame uhel smerujici od robota k cili
    double goalAngle = -Math.atan2(sprite.getX()-xDest, sprite.getY()-yDest );
    //rozdil uhlu = vysledny uhel o ktery musime otocit
    double missingAngle = goalAngle - currentAngle;
        
    //pokud je uhel vetsi nez PI tak to druhou stranou pujde urcite lip
    if(missingAngle>Math.PI){
    missingAngle = missingAngle-Math.PI*2;
    }
    if(missingAngle<-Math.PI){
    missingAngle = missingAngle+Math.PI*2;
    }
    System.out.println(" otoocime o uhel " +missingAngle);
    int brakeLength = (int)(sprite.engine.maxSpeed/sprite.gear.brakePower);
    int AccelerationLength = (int)(sprite.getMaxSpeed()/sprite.getAcceleration());
    int distance = (int)Math.sqrt((sprite.getX()-xDest)*(sprite.getX()-xDest)+(sprite.getY()-yDest)*(sprite.getY()-yDest));
    int travelLength = distance/(int)sprite.engine.maxSpeed;

//    s.setAction(new TurnAction(sprite, missingAngle,sprite.gear.turningSpeed));
    s.setAction(new AccelerateAction(sprite,System.currentTimeMillis(),runner));
    s.setAction(new KeepAction(travelLength-brakeLength-AccelerationLength));
    s.setAction(new BrakeAction(sprite, System.currentTimeMillis(),runner));
    
    }

}
